package com.gthncz;

import java.util.*;


public class Demo4 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * 输入的表达式的括号是否能配对
     * @param statement string字符串
     * @return bool布尔型
     */
    public boolean isValid (String statement) {
        Deque<Character> stack = new ArrayDeque<>();
        int len = statement.length();
        for (int i=0; i<len; ++i) {
            char c = statement.charAt(i);
            if (c == '(') {
                stack.push(c);
            }
            else if (c == ')') {
                if (stack.peek() == '(') {
                    stack.pop();
                } else {
                    return false;
                }
            }

            if (c == '[') {
                stack.push(c);
            }
            else if (c == ']') {
                if (stack.peek() == ']') {
                    stack.pop();
                } else {
                    return false;
                }
            }

            if (c == '{') {
                stack.push(c);
            }
            else if (c == '}') {
                if (stack.peek() == '{') {
                    stack.pop();
                } else {
                    return false;
                }
            }

        }

        return stack.isEmpty();
    }
}